package com.xuecheng.teaching.controller;

import com.xuecheng.api.teaching.WorkApi;
import com.xuecheng.api.teaching.model.dto.CompanyDTO;
import com.xuecheng.api.teaching.model.dto.WorkDTO;
import com.xuecheng.api.teaching.model.qo.QueryWorkModel;
import com.xuecheng.api.teaching.model.vo.WorkVo;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.teaching.common.util.UAASecurityUtil;
import com.xuecheng.teaching.convert.WorkConvert;
import com.xuecheng.teaching.service.WorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 作业 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class WorkController implements WorkApi {

    @Autowired
    private WorkService workService;

    @GetMapping("work/{workId}")
    public WorkDTO getWorkById(@PathVariable Long workId) {
        CompanyDTO company = UAASecurityUtil.getCompany();
        Long companyId = null;
        if (!ObjectUtils.isEmpty(company)) {
            companyId = company.getCompanyId();
        }

        return workService.getWorkById(workId, companyId);
    }

    @GetMapping("l/work/{workId}")
    public RestResponse<WorkDTO> getByWorkId(@PathVariable Long workId) {

        return workService.getByWorkId(workId);

    }

    @PostMapping("work")
    public WorkDTO createOrModifyWork(@RequestBody WorkVo workVo) {

        WorkDTO workDTO = WorkConvert.INSTANCE.workVoToDTO(workVo);
        String username = UAASecurityUtil.getUser().getUsername();
        Long companyId = UAASecurityUtil.getCompanyId();
        workDTO.setCompanyId(companyId);
        workDTO.setUsername(username);

        return workService.createOrModifyWork(workDTO);
    }

    @PostMapping("work/list")
    public PageVO<WorkDTO> queryWorkList(PageRequestParams params,
                                         QueryWorkModel model
    ) {

        Long companyId = UAASecurityUtil.getCompanyId();
        return workService.queryWorkList(params, model, companyId);
    }

    @DeleteMapping("work/{workId}")
    public void removeWork(@PathVariable Long workId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        workService.removeWork(workId, companyId);
    }

    /**
     * <p>
     * 微服务提供调用 ，修改作业信息
     * </p>
     *
     * @param params 数据封装Map 传参 courseId cname teachplanId pname
     * @return RestResponse
     */
    @PostMapping("l/work/association-info/{workId}")
    public RestResponse modifyWorkInfo(@RequestBody Map<String, Object> params,
                                       @PathVariable Long workId) {
        return workService.modifyWorkInfo(params, workId);
    }
}
